# coding=utf-8
# 实现黑名单装饰器

from untils.logger import log

import allure
from appium.webdriver.common.mobileby import By


def handle_black(function):
    def run(*args, **kwargs):
        from page.base_page import BasePage
        _black_list = [(By.XPATH, '//*[@resource-id="com.xueqiu.android:id/iv_close"]')]
        instance: BasePage = args[0]
        try:
            log.debug("find", args[1:])
            instance.wait_can_click((args[1], args[2]))
            return function(*args, **kwargs)
        except Exception as e:
            log.error("element not found")
            allure.attach(instance.screenshot(), attachment_type=allure.attachment_type.PNG)
            # 处理黑名单中的弹框
            for ele in _black_list:
                if len(instance.finds(*ele)) > 0:
                    instance.wait_can_click(ele)
                    instance.find(*ele).click()
                # 处理黑名单中元素后完之后再去查找目标元素
                instance.wait_can_click((args[1], args[2]))
                return function(*args, **kwargs)
            raise e

    return run
